﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ControlCenter.Sensors;
using log4net.Config;

namespace ControlCenter
{
    /// <summary>
    /// Written by: Salvador Richter
    /// Appache License Version 2.0 
    /// </summary>
    public partial class MainWindow : Form
    {
        public MainWindow()
        {
            InitializeComponent();
            XmlConfigurator.Configure();
            // disable tablet control on normal computer
            if (!DriveController.Instance().TabletEnabled())
            {
                this.tabControl1.TabPages.RemoveAt(0);
                this.tabPage1.Hide();
                this.tabPage1.Dispose();
            }
            this.InfoLabel.Text = DriveController.Instance().Message;
        }

        private void StartButton_Click(object sender, EventArgs e)
        {
            StartButton.Enabled = false;
            StopButton.Enabled = true;
            backgroundWorker1.WorkerReportsProgress = true;
            backgroundWorker1.WorkerSupportsCancellation = true;
            backgroundWorker1.RunWorkerAsync();
        }

        private void StopButton_Click(object sender, EventArgs e)
        {
            StartButton.Enabled = true;
            StopButton.Enabled = false;
            backgroundWorker1.CancelAsync();
        }

        // do work endless
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            DriveController controller = DriveController.Instance();
            while (!backgroundWorker1.CancellationPending)
            {
                controller.DriveByTiltingSensor();
                backgroundWorker1.ReportProgress(1, controller);
                e.Result = controller;
                System.Threading.Thread.Sleep(200);
            }
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            DriveController controller = e.UserState as DriveController;
            XValue.Text = controller.Reader.XValue.ToString();
            YValue.Text = controller.Reader.YValue.ToString();
            ZValue.Text = controller.Reader.ZValue.ToString();
            InfoLabel.Text = controller.Message;
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            DriveController controller = e.Result as DriveController;
            controller.StopDrive();
        }

        private void SubmitButton_Click(object sender, EventArgs e)
        {
            DriveController.Instance().SendRemoteCommand(ManualCommand.Text);
        }

        private void LoadCommandsButton_Click(object sender, EventArgs e)
        {
            AvailableCommandsCombo.Items.Clear();
            AvailableCommandsCombo.Items.AddRange(DriveController.Instance().ExecutableCommands());
        }

        private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
        {
            DriveController.Instance().Close();
        }

        private void AvailableCommandsCombo_SelectedIndexChanged(object sender, EventArgs e)
        {
            ManualCommand.Text = ((ComboBox)sender).Text;
        }

        private void StartCursorControlButton_Click(object sender, EventArgs e)
        {
            textBoxCursorControlKeyboardLitener.Focus();
        }

        /// <summary>
        /// textBoxCursorControl is behind start button. required just to have component for keyboard listener
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBoxCursorControl_KeyDown(object sender, KeyEventArgs e)
        {
            DriveController.Instance().DriveByCursor(e.KeyData);
            InfoLabel.Text = DriveController.Instance().Message;
        }

        private void StopCursorControlButton_Click(object sender, EventArgs e)
        {
            this.tabControl1.Focus();
            InfoLabel.Text = "Stop driving";
        }

        private void ReadSystemConfigButton_Click(object sender, EventArgs e)
        {
            SystemConfigListBox.Items.Clear();
            SystemConfigListBox.Items.AddRange(DriveController.Instance().ReadSystemConfig());
        }

        private void CheckStatusButton_Click(object sender, EventArgs e)
        {
            SystemConfigListBox.Items.Clear();
            SystemConfigListBox.Items.AddRange(DriveController.Instance().ReadSystemStatus());
        }

        private void ResetStatusButton_Click(object sender, EventArgs e)
        {
            SystemConfigListBox.Items.Clear();
            DriveController.Instance().ResetSystemStatus();
        }
    }
}
